{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T15:23:26.493869Z",
     "start_time": "2023-07-22T15:23:26.490035Z"
    }
   },
   "outputs": [],
   "source": [
    "# import modules\n",
    "import pickle\n",
    "import sys\n",
    "sys.path.append('..') \n",
    "# the data structure for blocksworld is defined in the parent directory\n",
    "from reasoners.visualization import visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-22T15:11:44.345695Z",
     "start_time": "2023-07-22T15:11:41.138585Z"
    }
   },
   "outputs": [],
   "source": [
    "# load the log\n",
    "with open(\"logs/v2_step8/algo_output/1.pkl\", 'rb') as f:\n",
    "    mcts_result = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# customize the information to show (optional)\n",
    "from reasoners.visualization.tree_snapshot import NodeData, EdgeData\n",
    "from reasoners.algorithm.mcts import MCTSNode\n",
    "\n",
    "def blocksworld_node_data_factory(n: MCTSNode) -> NodeData:\n",
    "    return NodeData({\"block state\": n.state.blocks_state if n.state else \"Not expanded\",\n",
    "                     \"# goals satisfied\": n.reward_details[\"goal_reached\"][1] if hasattr(n, \"reward_details\") else \"N/A\"})\n",
    "def blocksworld_edge_data_factory(n: MCTSNode) -> EdgeData:\n",
    "    return EdgeData({\"Q\": n.Q, \"intuition\": n.fast_reward_details[\"intuition\"], \"self_eval\": n.fast_reward_details[\"self_eval\"], \"action\": n.action})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Visualizer URL: https://www.llm-reasoners.net/visualizer/949690c5-7c60-4698-9842-40325a62125a?accessKey=14d8ebba\n"
     ]
    }
   ],
   "source": [
    "# let's go!\n",
    "visualize(mcts_result, node_data_factory=blocksworld_node_data_factory, edge_data_factory=blocksworld_edge_data_factory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rap",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
